package com.mMap.controller;

import com.mMap.annotation.GlobalInterceptor;
import com.mMap.annotation.VerifyParam;
import com.mMap.entity.constants.Constants;
import com.mMap.entity.dto.SessionWebUserDto;
import com.mMap.entity.enums.UserStatusEnum;
import com.mMap.entity.enums.VerifyRegexEnum;
import com.mMap.entity.po.MapInfo;
import com.mMap.entity.query.MapInfoQuery;
import com.mMap.entity.vo.PaginationResultVO;
import com.mMap.entity.vo.ResponseVO;
import com.mMap.service.MapInfoService;
import com.mMap.utils.StringTools;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class MapController extends ABaseController{
    @Resource
    private MapInfoService mapInfoService;
    @CrossOrigin(origins = Constants.FrontOrigins, allowCredentials = "true")
    @RequestMapping("mapList")
    @GlobalInterceptor(checkLogin=true,checkParams= true)
    public ResponseVO mapList(HttpSession session,@RequestBody MapInfoQuery query
    ) {
        try {
            SessionWebUserDto sessionWebUserDto = getUserInfoFromSession(session);
            System.out.println(sessionWebUserDto.getUserId());
            if (sessionWebUserDto.getRole()==0||sessionWebUserDto.getRole()==2&&query.getAllData()==null){
            query.setCreatorId(sessionWebUserDto.getUserId());
            } else {
            }
            query.setOrderBy("createTime desc");
            List<MapInfo> resultVO = mapInfoService.findListByParam(query);
            System.out.println(resultVO);
            return getSuccessResponseVO(resultVO);
        } finally {
            session.removeAttribute(Constants.CHECK_CODE_KEY);
        }
    }
    @RequestMapping("/addMap")
    @GlobalInterceptor(checkLogin=true,checkParams= true)
    public ResponseVO addMap(HttpSession session,
//                               @VerifyParam(required = true) String title,
//                               @VerifyParam(required = true) String remark,
                               @VerifyParam(required = true)  @RequestBody MapInfo requestData
    ) {
        try {
            System.out.println(requestData);
            SessionWebUserDto sessionWebUserDto = getUserInfoFromSession(session);
            MapInfo mapInfo = new MapInfo();
            mapInfo.setTitle(requestData.getTitle());
            mapInfo.setRemark(requestData.getRemark());
            mapInfo.setMapData(requestData.getMapData());
            mapInfo.setCreator(sessionWebUserDto.getNickName());
            mapInfo.setCreatorId(sessionWebUserDto.getUserId());
            mapInfo.setCreateTime(new Date());
            mapInfo.setStatus(UserStatusEnum.ENABLE.getStatus());

            System.out.println(mapInfo);
            System.out.println("mapId:"+requestData.getMapId());
            MapInfoQuery query = new MapInfoQuery();
            query.setCreatorId(sessionWebUserDto.getUserId());
            List<MapInfo> resultVO = mapInfoService.findListByParam(query);
            System.out.println("resultVO"+sessionWebUserDto.getRole());
            System.out.println(resultVO.size());
            if(sessionWebUserDto.getRole()==0&&resultVO.size()>5){
//                普通用户
            System.out.println("》5");
                return  getErrorResponseVO(null,"普通会员最多能创建6条记录");
            }
            else if(sessionWebUserDto.getRole()==2&&resultVO.size()>49){
            System.out.println("》50");
//                会员
                return  getErrorResponseVO(null,"会员最多能创建50条记录");
            }
            if (requestData.getMapId()!=null&&requestData.getMapId()!="")  {
                mapInfo.setMapId(requestData.getMapId());
                mapInfoService.updateMapInfoByMapId(mapInfo, requestData.getMapId());
            }
            else mapInfoService.add(mapInfo);

            return getSuccessResponseVO(null);
        } finally {
            session.removeAttribute(Constants.CHECK_CODE_KEY);
        }
    }
    @RequestMapping("/mapDetail")
    @GlobalInterceptor(checkLogin=true)
    public ResponseVO getMapDetail(HttpSession session,String id) {
//        SessionWebUserDto sessionWebUserDto = getUserInfoFromSession(session);
        MapInfo mapInfo = mapInfoService.getMapInfoByMapId(id);
        return getSuccessResponseVO(mapInfo);
    }
    @RequestMapping("/delMapById")
    @GlobalInterceptor(checkLogin=true)
    public ResponseVO delMapById(HttpSession session,@VerifyParam(required = true) String id) {
        SessionWebUserDto sessionWebUserDto = getUserInfoFromSession(session);
        MapInfo mapInfo  = mapInfoService.getMapInfoByMapId(id);
        Integer resultVO= null;
        Boolean isUserSelf = true;
        String uId = sessionWebUserDto.getUserId();
        String cId = mapInfo.getCreatorId();
        if (!(uId.equals(cId))) isUserSelf = false;
        System.out.println(isUserSelf+"()isuserSelf");
        if (isUserSelf&&sessionWebUserDto.getRole()==0 || sessionWebUserDto.getRole()==1) {
//            验证是否该账户文件/管理员
            resultVO = mapInfoService.deleteMapInfoByMapId(id);
        }
        return getSuccessResponseVO(resultVO);
    }

}

